import db from '../core/db';
import utils from './utils';
import logger from './logger';
import userClient from '../http/userClient';
import userInfo from '../model/user/userInfo';
import homeUserCardModel from '../model/user/homeUserCardModel';

export default class current {
  private static _instance: current = null;

  public static get instance(): current {
    if (this._instance == null)
      this._instance = new current();
    return this._instance;
  }

  private constructor() {
    this.loadInfo();
  }

  public loadInfo(): Promise<void> {
    return new Promise((resolve) => {
      try {
        db.instance.query('userid').then((value) => this._mid = utils.isEmpty(value?.toString()) ? 0 : parseFloat(value.toString()));
        db.instance.query('accesskey').then((value) => this._accesskey = value.toString());
        db.instance.query('user_name').then((value) => this.user_name = value.toString());
        db.instance.query('login_cookie').then((value) => {
          let str = value.toString();
          if (utils.isEmpty(str))
            this._cookies = `buvid3=${utils.buvid()}`;
          else
            this._cookies = str.replace('undefined', '');
        });
        db.instance.query('buvid').then((value) => {
          if (utils.isEmpty(value)) {
            value = utils.buvid();
            db.instance.update('buvid', value);
          }
          this._buvid = value.toString();
        });
        db.instance.query('csrf').then((value) => {
          this._csrf = value.toString();
        });
        db.instance.query('user_face').then((value) => {
          this.user_face = value.toString();
          resolve();
        });
      } catch (e) {
        logger.error(e);
      }
    });
  }

  public clear() {
    try {
      db.instance.update('csrf', '');
      db.instance.update('userid', '');
      db.instance.update('user_name', '');
      db.instance.update('accesskey', '');
      db.instance.update('user_face', '');
      db.instance.update('expires_in', '');
      db.instance.update('login_cookie', '');
      db.instance.update('refresh_token', '');
    } catch (e) {
      logger.error(e);
    }
  }

  private _userInfo: userInfo;

  public get userInfo(): Promise<userInfo> {
    return new Promise((resolve) => {
      try {
        if (utils.isEmpty(this._userInfo)) {
          userClient.userInfo(this._mid).then((res) => {
            if (res.success)
              this._userInfo = res.data;
            resolve(this._userInfo);
          });
        }
      } catch (e) {
        logger.error(e);
      }
      resolve(this._userInfo);
    });
  }

  private _userCard: homeUserCardModel;

  public get userCard(): Promise<homeUserCardModel> {
    return new Promise((resolve) => {
      try {
        if (utils.isEmpty(this._userCard)) {
          userClient.getMine().then((res) => {
            if (res.success)
              this._userCard = res.data;
            resolve(this._userCard);
          });
        }
      } catch (e) {
        logger.error(e);
      }
      resolve(this._userCard);
    });
  }

  private _cookies: string = '';

  public get cookies() {
    return this._cookies;
  }

  private _mid: number = 0;

  public get mid() {
    return this._mid;
  }

  private _csrf: string = '';

  public get csrf() {
    return this._csrf;
  }

  public get isLogin() {
    return!utils.isEmpty(this._mid) && this._mid > 0;
  }

  public get isVip() {
    if (this.isLogin) {

    }
    return false;
  }

  private user_face: string = '';

  public get userFace() {
    return this.user_face;
  }

  private user_name: string = '';

  public get userName() {
    return this.user_name;
  }

  private _accesskey: string = '';

  public get accesskey() {
    return this._accesskey;
  }

  private _buvid: string = '';

  public get buvid() {
    return this._buvid;
  }
}